package com.hxzy.three.controller; /**
 * @author wzm
 * @create 2023-09-20 14:45
 * project_name:javaweb2308
 */

import com.demo.JDBCUtils;
import com.hxzy.three.entity.User;
import com.hxzy.three.service.UserService;
import com.hxzy.three.service.impl.UserServiceImpl;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

//@WebServlet("/register")

@WebServlet("/login")
public class UserController extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      /*  //1.获取注册的参数  username password
        //2.调用注册的方法将参数传过去
        boolean register = userService.register(new User());
        //3.判断返回的结果
        if (register){
            //成功去登录页
        }else{
            //失败
        }*/


        //模拟登录
        //1.设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;chatset=UTF-8");

        //2.获取请求过来的用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

//        3.调用Service完成登录
        User login = new User();//封装页面过来的数据称为一个对象

       User user =  userService.login(login);

        //用用户输入的信息和数据对比

        //4.根据对比结果 响应具体的内容
        if(user != null){
            request.getRequestDispatcher("/index.jsp").forward(request,response);
        }else{
            //登陆失败 留在登录页面
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }



    }



}
